package org.nch.videoLauncher.view;

import org.nch.videoLauncher.presenter.NavButtonPresenter;

import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.File;

/**
 * Created by IntelliJ IDEA.
 * User: bill
 * Date: Nov 18, 2008
 * Time: 11:31:15 AM
 * To change this template use File | Settings | File Templates.
 */
public class NavButtonView implements IContentPanel, INavButtonView{
    private JPanel mainPanel;
    private JButton browseButton;
    private JButton historyButton;
    private JButton viewRecentVideosButton;
    private JButton selectFileButton;
    private JButton viewFavoritesButton;
    private JButton addEditVideosButton;
    private JButton profilesButton;
    private JButton searchButton;
    private JButton randomButton;

    private NavButtonPresenter presenter;
    private String selectedFilename;

    public NavButtonView() {
        addEditVideosButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                // delegate action to the presenter
                presenter.addEditButtonPressed();
            }
        });
        browseButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                // delegate action to the presenter
                presenter.browseVideoButtonPressed();
            }

        });
        selectFileButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {

                // delete action to the presenter
                presenter.selectFileButtonPressed();
            }
        });
        profilesButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.profileButtonPressed();
            }
        });
        historyButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.historyButtonPressed();
            }
        });
        searchButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent event) {
                presenter.searchButtonPressed();
            }
        });
    }

    public JPanel getRootComponent() {

        return mainPanel;
    }

    public void setPresenter(NavButtonPresenter navButtonPresenter){
        this.presenter = navButtonPresenter;
    }

    public boolean isFileSelected(String baseVideoPath){

        File file = new File(baseVideoPath);
        JFileChooser jfc = new JFileChooser(file);
        jfc.setDialogType(JFileChooser.OPEN_DIALOG);
        jfc.setFileSelectionMode(JFileChooser.FILES_ONLY);
        jfc.setMultiSelectionEnabled(false);
        Integer returnValue = jfc.showOpenDialog(null);
        if (returnValue == JFileChooser.APPROVE_OPTION){
            file=jfc.getSelectedFile();
            //  this.selectedFilename = file.getAbsolutePath();
            this.selectedFilename = file.getAbsolutePath().substring(baseVideoPath.length()+1, file.getAbsolutePath().length());
            return true;
        }
        else
            return false;
    }

    public String getSelectedFilename() {
        return this.selectedFilename;
    }

}
